<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="出库单号" prop="outboundNo">
        <el-input
          v-model="queryParams.outboundNo"
          placeholder="请输入出库单号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="出库日期" prop="outboundDate">
        <el-date-picker
          clearable
          v-model="queryParams.outboundDate"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择出库日期"
        >
        </el-date-picker>
      </el-form-item>

      <el-form-item label="状态" prop="status">
        <el-select
          v-model="queryParams.status"
          placeholder="请选择状态"
          clearable
        >
          <el-option
            v-for="dict in dict.type.tb_outbound_order"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>

      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>
    <right-toolbar
      :showSearch.sync="showSearch"
      @queryTable="getList"
    ></right-toolbar>
    <!-- <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['depot:outboundOrder:add']"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['depot:outboundOrder:edit']"
          >修改</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['depot:outboundOrder:remove']"
          >删除</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['depot:outboundOrder:export']"
          >导出</el-button
        >
      </el-col> -->

    <!-- </el-row> -->

    <el-table
      v-loading="loading"
      :data="outboundOrderList"
      @selection-change="handleSelectionChange"
      row-key="outboundId"
    >
      <el-table-column type="expand">
        <template slot-scope="props">
          <el-table :data="props.row.detailList || []" style="width: 100%">
            <el-table-column label="SKU" prop="sku" align="center">
              <template slot-scope="scope">
                {{ scope.row.sku || "-" }}
              </template>
            </el-table-column>
            <el-table-column label="产品名称" prop="productName" align="center">
              <template slot-scope="scope">
                {{ scope.row.productName || "-" }}
              </template>
            </el-table-column>
            <el-table-column label="出库数量" prop="quantity" align="center">
              <template slot-scope="scope">
                {{ scope.row.quantity || "-" }}
              </template>
            </el-table-column>
            <el-table-column label="单个成本" align="center">
              <template slot-scope="scope">
                {{ calculateUnitPrice(scope.row) }}
              </template>
            </el-table-column>
            <el-table-column label="总成本" prop="amount" align="center">
              <template slot-scope="scope">
                {{ scope.row.amount || "-" }}
              </template>
            </el-table-column>
          </el-table>
        </template>
      </el-table-column>

      <el-table-column label="出库单号" align="center" prop="outboundNo">
        <template slot-scope="scope">
          {{ scope.row.outboundNo || "-" }}
        </template>
      </el-table-column>

      <el-table-column label="关联单据号" align="center" prop="relatedOrderNo">
        <template slot-scope="scope">
          {{ scope.row.relatedOrderNo || "-" }}
        </template>
      </el-table-column>

      <el-table-column label="仓库" align="center" prop="warehouseName">
        <template slot-scope="scope">
          {{ scope.row.warehouseName || "-" }}
        </template>
      </el-table-column>

      <el-table-column label="出库类型" align="center" prop="outboundType">
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.tb_outbound_order_type"
            :value="scope.row.outboundType"
            empty-text="-"
          />
        </template>
      </el-table-column>

      <el-table-column
        label="出库日期"
        align="center"
        prop="outboundDate"
        width="180"
      >
        <template slot-scope="scope">
          <span>{{
            scope.row.outboundDate
              ? parseTime(scope.row.outboundDate, "{y}-{m}-{d}")
              : "-"
          }}</span>
        </template>
      </el-table-column>

      <el-table-column label="出库总数量" align="center" prop="totalQuantity">
        <template slot-scope="scope">
          {{ scope.row.totalQuantity || "-" }}
        </template>
      </el-table-column>

      <el-table-column label="出库总成本" align="center" prop="totalAmount">
        <template slot-scope="scope">
          {{ scope.row.totalAmount || "-" }}
        </template>
      </el-table-column>

      <el-table-column label="状态" align="center" prop="status">
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.tb_outbound_order"
            :value="scope.row.status"
            empty-text="-"
          />
        </template>
      </el-table-column>

      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="success"
            icon="el-icon-check"
            v-hasPermi="['depot:outboundOrder:confirm']"
            @click="handleConfirmOutbound(scope.row)"
            v-if="scope.row.status !== 1"
            class="rainbow-btn"
            >确认出库</el-button
          >
        </template>
      </el-table-column>
      <!-- <el-table-column label="备注" align="center" prop="remark" /> -->
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
  </div>
</template>

<script>
import {
  listOutboundOrder,
  getOutboundOrder,
  delOutboundOrder,
  addOutboundOrder,
  updateOutboundOrder,
  confirmOutboundOrder,
} from "@/api/depot/outboundOrder";

export default {
  name: "OutboundOrder",
  dicts: ["tb_outbound_order", "tb_outbound_order_type"],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 出库单表格数据
      outboundOrderList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        outboundNo: null,
        warehouseId: null,
        outboundType: null,
        outboundDate: null,
        relatedOrderNo: null,
        customerId: null,
        receiverName: null,
        receiverPhone: null,
        receiverAddress: null,
        totalQuantity: null,
        totalAmount: null,
        status: null,
        approver: null,
        approveTime: null,
        creator: null,
        updater: null,
        deleteStatus: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      // rules: {
      //   outboundNo: [
      //     { required: true, message: "出库单号不能为空", trigger: "blur" }
      //   ],
      //   warehouseId: [
      //     { required: true, message: "仓库ID不能为空", trigger: "blur" }
      //   ],
      //   outboundType: [
      //     { required: true, message: "出库类型(01:销售出库,02:调拨出库,03:其他出库)不能为空", trigger: "change" }
      //   ],
      //   outboundDate: [
      //     { required: true, message: "出库日期不能为空", trigger: "blur" }
      //   ],
      // }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询出库单列表 */
    getList() {
      this.loading = true;
      listOutboundOrder(this.queryParams).then((response) => {
        this.outboundOrderList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        outboundId: null,
        outboundNo: null,
        warehouseId: null,
        outboundType: null,
        outboundDate: null,
        relatedOrderNo: null,
        customerId: null,
        receiverName: null,
        receiverPhone: null,
        receiverAddress: null,
        totalQuantity: null,
        totalAmount: null,
        status: null,
        remark: null,
        approver: null,
        approveTime: null,
        creator: null,
        createTime: null,
        updater: null,
        updateTime: null,
        deleteStatus: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.outboundId);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加出库单";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const outboundId = row.outboundId || this.ids;
      getOutboundOrder(outboundId).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改出库单";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.outboundId != null) {
            updateOutboundOrder(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addOutboundOrder(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const outboundIds = row.outboundId || this.ids;
      this.$modal
        .confirm('是否确认删除出库单编号为"' + outboundIds + '"的数据项？')
        .then(function () {
          return delOutboundOrder(outboundIds);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "depot/outboundOrder/export",
        {
          ...this.queryParams,
        },
        `outboundOrder_${new Date().getTime()}.xlsx`
      );
    },
    /** 确认出库操作 */
    handleConfirmOutbound(row) {
      this.$modal
        .confirm('确认要将出库单"' + row.outboundNo + '"出库吗？')
        .then(function () {
          return confirmOutboundOrder(row.outboundId);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("确认出库成功");
        })
        .catch(() => {});
    },
    // 计算单个成本
    calculateUnitPrice(row) {
      if (!row.amount || !row.quantity || row.quantity === 0) {
        return "-";
      }
      const unitPrice = (row.amount / row.quantity).toFixed(4);
      return unitPrice;
    },
  },
};
</script>
<!-- <style>
.el-button.rainbow-btn,
.el-button.rainbow-btn:focus,
.el-button.rainbow-btn:hover,
.el-button.rainbow-btn:active {
  background-image: linear-gradient(
    124deg,
    #ff0000,
    #ff7f00,
    #ffff00,
    #00ff00,
    #00ffff,
    #0000ff,
    #8b00ff
  ) !important;

  /* 关键修改：将尺寸从1400%减小到一个更合理的值 */
  background-size: 300% 300% !important; /* 尝试一个较小的值，比如300% */

  animation: rainbow 10s ease infinite !important; /* 可以适当调整动画时间以达到最佳效果 */
  border: none !important;
  color: white !important;
}

@keyframes rainbow {
  0% {
    background-position: 0% 82%;
  }
  50% {
    background-position: 100% 19%;
  }
  100% {
    background-position: 0% 82%;
  }
}
</style> -->
